package com.example.rentingapp.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;

import com.example.rentingapp.R;
import com.example.rentingapp.activity.LoginActivity;
import com.example.rentingapp.activity.MyCollectionActivity;
import com.example.rentingapp.activity.MyDemandActivity;
import com.example.rentingapp.activity.MyHouseActivity;
import com.example.rentingapp.activity.MyInformationActivity;
import com.example.rentingapp.activity.MyOrderActivity;
import com.example.rentingapp.activity.MySettingActivity;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationEntity;
import com.example.rentingapp.enetity.InformationResponse;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MineFragment extends BaseFragment implements View.OnClickListener {
    private CardView cvMyCollect, cvMyOrder, cvMyHouse, cvMyDemand;
    private LinearLayout llMyInformation, llMySetting, llExitLogin;
    private TextView tvName, tvPhone;
    private String informationUserId, name, phone;
    private List<InformationEntity> datas = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    tvName.setText(datas.get(0).getName());
                    tvPhone.setText(String.valueOf(datas.get(0).getPhone()));
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.fragment_mine;
    }

    //跳转我的收藏
    @Override
    protected void initView() {
        cvMyCollect = mRootView.findViewById(R.id.cv_my_collect);
        cvMyHouse = mRootView.findViewById(R.id.cv_my_house);
        cvMyDemand = mRootView.findViewById(R.id.cv_my_demand);
        cvMyOrder = mRootView.findViewById(R.id.cv_my_order);
        llMyInformation = mRootView.findViewById(R.id.ll_my_information);
        llMySetting = mRootView.findViewById(R.id.ll_my_setting);
        llExitLogin = mRootView.findViewById(R.id.ll_exit_login);

        tvName = mRootView.findViewById(R.id.tv_my_name);
        tvPhone = mRootView.findViewById(R.id.tv_my_phone);
    }

    @Override
    protected void initData() {
        cvMyCollect.setOnClickListener(this);
        llMyInformation.setOnClickListener(this);
        cvMyHouse.setOnClickListener(this);
        cvMyOrder.setOnClickListener(this);
        cvMyDemand.setOnClickListener(this);
        llMySetting.setOnClickListener(this);
        llExitLogin.setOnClickListener(this);

        informationUserId = findByKey("user_id");
        searchMyInformation();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cv_my_collect:
                navigateTo(MyCollectionActivity.class);
                break;
            case R.id.cv_my_house:
                navigateTo(MyHouseActivity.class);
                break;
            case R.id.cv_my_demand:
                navigateTo(MyDemandActivity.class);
                break;
            case R.id.cv_my_order:
                navigateTo(MyOrderActivity.class);
                break;
            case R.id.ll_my_information:
                navigateTo(MyInformationActivity.class);
                break;
            case R.id.ll_my_setting:
                navigateTo(MySettingActivity.class);
                break;
            case R.id.ll_exit_login:
                removeByKey("logged");
                navigateToWithFlag(LoginActivity.class,
                        Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                break;
        }
    }

    private void searchMyInformation() {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("information_user_id", informationUserId);//从本地获取user_id
        Api.config(ApiConfig.FIND_INFORMATION, params).postRequest(getActivity(), new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess", res);
                Gson gson = new Gson();
                InformationResponse response = gson.fromJson(res, InformationResponse.class);
                if (response.getSuccess() == true) {
                    List<InformationEntity> list = response.getDatas();
                    if (list != null && list.size() > 0) {
                        datas = list;
                        mHandler.sendEmptyMessage(0);
                    }
                } else {
                    showToastSync("个人信息未完善");
                }
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}
